package com.nd.service.wx;

import cn.hutool.crypto.digest.BCrypt;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nd.OSS.AliyunLetter;
import com.nd.dao.UserMapper;
import com.nd.pojo.bean.User;
import com.nd.pojo.bean.UserExample;
import com.nd.pojo.bo.wx.UserRegisterBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserRegisterServiceImpl implements UserRegisterService{
    @Autowired
    UserMapper userMapper;

    @Override
    public String regCaptcha(String mobile) {
        String msg = "123456";
        try {
            msg = AliyunLetter.sendMsg(mobile);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @Override
    public Integer register(UserRegisterBo userRegisterBo) {
        User user = new User();
        String mobile = userRegisterBo.getMobile();
        String username = userRegisterBo.getUsername();
        UserExample userExample1 = new UserExample();
        userExample1.createCriteria().andMobileEqualTo(mobile);
        List<User> users1 = userMapper.selectByExample(userExample1);
        if(users1.size()>0) return 9999;//该手机号已注册
        UserExample userExample2 = new UserExample();
        userExample2.createCriteria().andUsernameEqualTo(username);
        List<User> users2 = userMapper.selectByExample(userExample2);
        if(users2.size()>0) return 9998;//用户名不可用
        user.setUsername(username);
        String password = userRegisterBo.getPassword();
        String encodePassword = BCrypt.hashpw(password, BCrypt.gensalt());
        user.setPassword(encodePassword);
        user.setNickname(userRegisterBo.getUsername());
        user.setMobile(mobile);
        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        user.setWeixinOpenid(userRegisterBo.getWxCode());
        Date now = new Date();
        user.setAddTime(now);
        user.setUpdateTime(now);
        int register = userMapper.insertSelective(user);
        if(register == 0) return 10000;
        return 0;
    }

    @Override
    public Integer reset(UserRegisterBo userRegisterBo) {
        String mobile = userRegisterBo.getMobile();
        String password = userRegisterBo.getPassword();
        UserExample userExample = new UserExample();
        userExample.createCriteria().andMobileEqualTo(mobile);
        List<User> users = userMapper.selectByExample(userExample);
        if(users.size() == 0) return 9999;//该手机号用户不存在,请先注册！
        User updateUser = users.get(0);
        Date now = new Date();
        String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
        updateUser.setPassword(hashpw);
        updateUser.setUpdateTime(now);
        int update = userMapper.updateByPrimaryKeySelective(updateUser);
        if(update == 0) return 10000;
        return 0;
    }
}
